/*
 * A simple ToDo Manager
 *
 * Copyright (c) 2011 Nosheen Zaza, Manuel Rebollo, Seyedsaeid Mahpeikar and
 * Wo Ke
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package todomanager.ui.listview;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ListModel;
import todomanager.model.Task;

/**
 * This is the list model for a {@code JList} that contains tasks.
 * 
 * @author
 * Nosheen Zaza
 */
public class TaskListModel extends DefaultListModel implements ListModel{

    private List<Task> tasks;

    /**
     * Creates a new instance and loads the given tasks
     * @param tasks
     */
    public TaskListModel(List<Task> tasks) {
        loadTasks(tasks);
    }

    /**
     * Updates the task list.
     * @param tasks
     */
    public void updateModel(List<Task> tasks) {
        loadTasks(tasks);
    }

    /**
     * Loads tasks among the passed task list  
     * that are unfinished, and have the date of today.
     * @param tasks
     */
    private void loadTasks(List<Task> tasks) {
        this.tasks = new ArrayList<Task>();
        GregorianCalendar today = new GregorianCalendar();
        for (Task task : tasks) {

            if (task.getDueDate() != null && task.getDueDate().get(
                    GregorianCalendar.DAY_OF_YEAR)
                    == today.get(GregorianCalendar.DAY_OF_YEAR)
                    && !task.isDone()) {
                this.tasks.add(task);
            }
        }
        this.fireContentsChanged(this, 0, this.tasks.size());
    }

    /**
     * Returns the size of the task list.
     * @return Number of tasks in the task list of the model.
     */
    @Override
    public int getSize() {
        return tasks.size();
    }

    /**
     * Returns an element in the model at the specified index.
     * @param index
     * @return A task at the specified index.
     */
    @Override
    public Object getElementAt(int index) {
        return tasks.get(index);
    }
}
